package cn.xclink.kernel.service;

import java.util.ArrayList;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.xclink.common.mapper.BaseMapper;
import cn.xclink.common.service.BaseService;
import cn.xclink.kernel.entity.Role;
import cn.xclink.kernel.entity.RoleResource;
import cn.xclink.kernel.entity.UserRole;
import cn.xclink.kernel.mapper.RoleMapper;

@Service
public class RoleService extends BaseService<Role> {

	@Autowired
	private RoleMapper mapper;
	
	@Autowired
	private UserRoleService userRoleService;
	
	@Autowired
	private RoleResourceService roleResourceService;
	
	@Override
	public BaseMapper<Role> getMapper() {
		return mapper;
	}
	
	@Override
	public List<Role> find(Role role) {
		return mapper.selectExt(role);
	}

	/**
	 * 根据用户ID查找对应的角色对象
	 * @param userId
	 * @return  
	 */
	public List<Role> findRoleByUserId(Long userId) {
		return mapper.selectRoleByUserId(userId);
	}
	
	/**
	 * 保存角色
	 * @param user
	 */
	public int saveRole(Role role) throws Exception {
		
		int result = super.save(role);
		RoleResource droleResource = new RoleResource();
		droleResource.setRoleId(role.getRoleId());
		roleResourceService.deleteByProperties(droleResource);
		List<Long> ressourceIds = Arrays.asList(role.getResourceIds());
		for(Long resourceId : ressourceIds){
			RoleResource roleResource = new RoleResource();
			roleResource.setResourceId(resourceId);
			roleResource.setRoleId(role.getRoleId());
			roleResource.setAvailable("Y");
			roleResourceService.save(roleResource);
		}
		return result;
		
	}
	
	/**
	 * 编辑用户
	 */
	public int updateRole(Role role) throws Exception {
		
		int result = super.updateNotNull(role);
		RoleResource droleResource = new RoleResource();
		droleResource.setRoleId(role.getRoleId());
		roleResourceService.deleteByProperties(droleResource);
		List<Long> ressourceIds = Arrays.asList(role.getResourceIds());
		for(Long resourceId : ressourceIds){
			RoleResource roleResource = new RoleResource();
			roleResource.setResourceId(resourceId);
			roleResource.setRoleId(role.getRoleId());
			roleResource.setAvailable("Y");
			roleResourceService.save(roleResource);
		}
		return result;
	}
	
	@Override
	public int delete(Object key) throws Exception {
		Long roleId =  Long.parseLong(key+"");
		//1.校验是否有用户属于该角色
		UserRole userRole = new UserRole();
		userRole.setRoleId(roleId);
		if(userRoleService.find(userRole).size() > 0){
			throw new Exception("该角色已经分配给了用户，不允许删除！");
		}
		
		//2.删除角色对应的资源
		RoleResource roleResource = new RoleResource();
		roleResource.setRoleId(roleId);
		roleResourceService.deleteByProperties(roleResource);
		return super.delete(roleId);
	}
	
	/**
	 * 根据用户ID查找用户对应的角色
	 * @param userId
	 * @return
	 */
	public Set<String> findUserRoleNameByUserId(Long userId) {
		Set<String> roleSet = new HashSet<String>();
		List<Role> roleList = findRoleByUserId(userId);
		for (Role role : roleList) {
			roleSet.add(role.getRoleName());
		}
		return roleSet;
	}
	
	public List<Role> getRoleTree(List<Role> roleList,List<Role> accessRoleList){
		for(Role role : roleList){
			for(Role accessOrg : accessRoleList){
				if(role.getRoleId() == accessOrg.getRoleId()){
					role.setChecked(true);
					break;
				}
			}
		}
		return roleList;
	}
	
	public String[] findRoleIds(List<Role> roleList){
		List<String> list = new ArrayList<String>();
		for(Role role : roleList){
			list.add(role.getRoleId()+"");
		}
		return (String[])list.toArray(new String[list.size()]);
	}
}
